Skip to content

Expose MeshGL.merged() to recompute lost merge vectors - #3

Merged
tomasf merged 1 commit into
tomasf:mainfrom
gsdali:expose-meshgl-merge
Jun 15, 2026
Merged

tomasf merged 1 commit into
tomasf:mainfrom
gsdali:expose-meshgl-merge

Conversation

@gsdali

@gsdali gsdali commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

What

Exposes a Swift MeshGL.merged() that wraps the existing C++ MeshGL::Merge(). That helper is currently unreachable from the Swift API — MeshGL is constructed but its Merge() is never surfaced, and Manifold.init(_:) doesn't merge.

Why

Manifold judges manifoldness by shared vertex indices, not position. A surface that is closed geometrically but carries split/unmerged coincident vertices therefore reports a non-manifold status, and try Manifold(meshGL) throws — even though the geometry is watertight.

This is exactly the case the C++ Merge() doc comment calls out: "the case where a manifold MeshGL was produced, but its merge vectors were lost due to a round-trip through a file format." It's the common situation when ingesting STL (and other formats that don't carry merge vectors): every triangle brings its own copies of shared corners. Today there's no way to recover from Swift without dropping to the C bindings.

merged() returns a copy with the merge vectors recomputed, so the mesh is accepted:

let solid = try Manifold(meshGLFromSTL.merged())

API

public extension MeshGL {
    /// Returns a copy whose merge vectors are recomputed so that topologically identical but
    /// separated vertices are reconnected. No-op when nothing needs merging.
    func merged() -> MeshGL
}

Non-mutating (returns a copy), matching the immutable MeshGL value type. If you'd prefer a different shape — a mutating @discardableResult func merge() -> Bool mirroring the C++ return, or auto-merging inside Manifold.init(_:) — happy to adjust. I went with the least-invasive option that doesn't change existing construction behaviour.

Tests

  • A closed tetrahedron whose 4 corners are split across faces (12 vertices) is rejected by Manifold(_:) before merged() and accepted after, with the correct volume (1/6).
  • merged() on an already-connected cube is a no-op (volume preserved).

Full suite green locally (81 tests, macOS).

Context

Found while consuming manifold-swift from a mesh-reconstruction pipeline (ref #2). slice/trim/projection/properties all had Swift equivalents — Merge() was the only piece we still needed the raw C API for.

Wraps the existing C++ MeshGL::Merge() helper, which is otherwise
unreachable from Swift. Manifoldness is judged by shared vertex indices,
not position, so a surface that is closed geometrically but carries split
coincident vertices (e.g. after an STL round-trip that drops the merge
vectors) is reported non-manifold and `Manifold(_:)` throws. `merged()`
snaps those vertices back together so the mesh is accepted.

Adds two tests: a split tetrahedron that is rejected before merged() and
accepted (with correct volume) after, and a no-op check on an already
connected cube.
@tomasf
tomasf merged commit 5f21e5a into tomasf:main Jun 15, 2026
3 checks passed
@tomasf

tomasf commented Jun 15, 2026

Copy link
Copy Markdown
Owner

This looks great and makes sense. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants